#!/bin/bash

function failed() {
	echo "$@"
	exit 1
}

cd `dirname $0`
BASE_DIR=$PWD
export DYNCALL_HOME=$BASE_DIR/dyncall

DYNCALL_DIFF=$BASE_DIR/src/main/cpp/bridj/dyncall.diff

if [[ ! -d "$DYNCALL_HOME" ]]
then
	$BASE_DIR/admin/checkout_and_patch_dyncall.sh $DYNCALL_DIFF || failed "Failed to checkout and patch dyncall"
fi

cd $DYNCALL_HOME
hg diff > $DYNCALL_DIFF
cd $BASE_DIR

MAKE_CMD=make
if [[ "`which gmake`" != "" ]]; then
	MAKE_CMD=gmake
fi

UNAME_ARCH=`uname -m`
UNAME_OS=`uname -s | sed s/_NT-.*/_NT/`

function get_arch() {
    if [[ -n "$ARCH" ]]; then
        echo "$ARCH"
    elif [[ "$UNAME_OS" == "SunOS" ]]; then
        if [ "`isainfo -n`" = "amd64" ]; then
		    echo "x64"
	    else
		    echo "x86"
	    fi
    elif [[ "$UNAME_OS" == "Darwin" ]]; then
	    echo "universal"
    else
        case $UNAME_ARCH in
        i86pc|i386|i486|i586|i686|x86)
	        echo x86
	        ;;
        x86_64|amd64)
	        echo x64
	        ;;
        arm*)
			    if [[ -d /lib/arm-linux-gnueabihf ]] ; then
	        	echo "armhf"
	        elif [[ -d /lib/arm-linux-gnueabi ]] ; then
	        	echo "armel"
    	    else
    		    failed "Unknown Linux arm flavour: please edit $0 and Platform.java"
	        fi
	        ;;
        esac
    fi
}

function get_os() {
    case $UNAME_OS in
    WindowsNT)
	    echo "windows"
	    ;;
    Darwin)
	    echo "darwin"
	    ;;
    Linux)
	    echo "linux"
	    ;;
    SunOS)
	    echo "sunos"
	    ;;
    OpenBSD)
	    echo "openbsd"
	    ;;
    FreeBSD|GNU/kFreeBSD)
	    echo "freebsd"
	    ;;
    NetBSD)
	    echo "netbsd"
	    ;;
    esac
}


function get_os_arch() {
    case $OS in
    windows)
	    case $ARCH in
	    x86)
	        echo win32
	        ;;
	    x64)
	        echo win64
	        ;;
	    esac
	    ;;
    *)
        echo ${OS}_${ARCH}
        ;;
    esac
}

CMDS=$@
if [[ -z "$BRIDJ_TARGETS" ]]; then
	BRIDJ_TARGETS="default"
	if [[ -n "$ANDROID_NDK_HOME" ]] ; then
		BRIDJ_TARGETS="$BRIDJ_TARGETS android" ;
	fi
fi

if [[ "$1" != "clean" && "$FAST" != "1" ]]; then
    mvn generate-sources
fi

function build_libraries() {
    export NATIVE_ARCH=`ARCH="" get_arch`
    export ARCH=`get_arch`
    export OS_ARCH=`get_os_arch`
   
    for BRIDJ_TARGET in $BRIDJ_TARGETS ; do
	    case $BRIDJ_TARGET in
	    default)
		    cd $DYNCALL_HOME
		    local DYNCALL_CONFIGURE_ARGS=""
		    if [[ "$ARCH" != "$NATIVE_ARCH" ]]; then
		        DYNCALL_CONFIGURE_ARGS="--target-$ARCH"
	        fi
		    sh ./configure $DYNCALL_CONFIGURE_ARGS
		    $MAKE_CMD $@ || failed "Failed building dyncall"

		    export DYNCALL_HOME
		    export OS
		    export ARCH
		    export CONFIG
		    export OUT_DIR=$BASE_DIR/src/main/resources/org/bridj/lib/$OS_ARCH
		    export OBJ_DIR=target/build/${OS}_${ARCH}_${CONFIG}
		    export DYNCALL_BUILD_DIR=build_out/${OS}_${ARCH}_gcc_${CONFIG}

		    cd $BASE_DIR/src/main/cpp/bridj
		    $MAKE_CMD $@ || failed "Build of lib failed"

		    export OUT_DIR=$BASE_DIR/src/test/resources/org/bridj/lib/$OS_ARCH

		    cd $BASE_DIR/src/test/cpp/test
		    $MAKE_CMD $@ || failed "Build of test lib failed"

		    cd $BASE_DIR/src/test/cpp/dependsOnTest
		    $MAKE_CMD $@ || failed "Build of dependsOnTest lib failed"
		    ;;
	    android)
		    $BASE_DIR/src/main/cpp/bridj/MakeAndroid.sh $@
		    ;;
	    esac
    done
}

export OS=`get_os`

if [[ "$DEBUG" == "1" ]]; then
	CONFIG=debug
else
	CONFIG=release
fi

if [[ $OS =~ sunos ]]; then
    ARCH=x86 build_libraries $@
    ARCH=x64 build_libraries $@
else
    build_libraries $@
fi
